package com.lb.springboot.config.datasource;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

/**
 * mybatis配置
 * <p>@Configuration代表这个类等同于XML</p>
 * 此类和application.properties配置做的事情是一样的，二选一即可。
 * @author lb
 *
 */
//@Configuration
public class MybatisDataSourceConfig {
//
//	@Autowired
//	private DataSource dataSource;
//
//	@Bean(name="sqlSessionFactory")
//	public SqlSessionFactory sqlSessionFactoryBean() {
//		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//		bean.setDataSource(dataSource);
//		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//		try {
//			// 添加mybatis mapping文件的目录
//			bean.setMapperLocations(resolver.getResources("classpath*:com/lb/springboot/mapping/*.xml"));
//			SqlSessionFactory sqlSessionFactory = bean.getObject();
//			sqlSessionFactory.getConfiguration().setCacheEnabled(Boolean.TRUE);
//			return sqlSessionFactory;
//		} catch (Exception e) {
//			throw new RuntimeException(e);
//		}
//	}
//
//	@Bean
//	public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
//		return new SqlSessionTemplate(sqlSessionFactory);
//	}

}
